/**
*      author: Y_Yao
*      created: 2025-03-01 20:38:02
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 7;
unordered_set<int> s[N];
int allto[N];
int toall[N];
int n,q;
unordered_map<int,int> g_c;
unordered_map<int,int> c_n;

void solve()
{
    cin >> n >> q;
    for(int i = 1;i <= n;i++)
    {
        a[i].insert(i);
        g_c[i] = i;
        c_n[i] = 1;
        allto[i] = i;
        toall[i] = i;
    }
    while(q--)
    {
        int op;
        cin >> op;
        if(op == 1)
        {
            int a,b;
            cin >> a >> b;
            g_c[a] = b;
        }
        else if(op == 2)
        {
            int a,b;
            cin >> a >> b;
            
            swap(s[a],s[b]);
        }
        else
        {
            int a;
            cin >> a;
            int ans = allto[g_c[a]];
            cout << ans << endl;
        }
    }

}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}